

libname IA_INP "D:\Data\Hospital Inpatient Records\Hospital Iowa (2004-2009)";
libname WI_INP "D:\Data\Hospital Inpatient Records\Hospital Wisconsin (2004-2009)";

libname AHA "C:\Data\AHA Survey";

%let keep_v1 = bmonth byear amonth ayear ECODE1 DX1 FEMALE DISPUNIFORM PAY1 DSHOSPID;


%macro pull_s(lib,file1,file2,file3,keep_v2,keep_v3);
data junk1 (drop = l_amonth l_DSHOSPID l2_DSHOSPID l2_amonth);
   format state $2.;
   set &lib..&file1. (keep = &keep_v1. &keep_v2. &keep_v3. );
   file = "&file1.";
   state = substr("&lib.",1,2);
   if substr("&lib.",4,2) = 'IN' then inpatient = 1; else inpatient = 0; 
   l_amonth = lag(amonth);
   l_DSHOSPID = lag(DSHOSPID);
   l2_amonth = lag2(amonth);
   l2_DSHOSPID = lag2(DSHOSPID);
   if inpatient = 0 and female = . and amonth = . and state = 'CA' and 
      l_amonth ne . and l_DSHOSPID = DSHOSPID then amonth = l_amonth; 
   if inpatient = 0 and female = . and amonth = . and state = 'CA' and 
      l_amonth = . and l2_DSHOSPID = DSHOSPID then amonth = l2_amonth; 
   months_23 = intck('month',mdy(bmonth,14,byear+23),mdy(amonth,15,ayear));
   if months_23 = . then bad_date = 1; else bad_date = 0;
   if inpatient = 1 and (&keep_v2. = "1" or &keep_v3. = "7") then from_ED = 1; else from_ED = 0;
   if inpatient = 0 and DISPUNIFORM = 2 then trans_to_hosp = 1; else trans_to_hosp = 0;  
   if DISPUNIFORM = 20 then died = 1; else died = 0;
   if substr(DX1,1,2) in ('63','64','65','66','67') then preg_related = 1; else preg_related = 0;
   if pay1 = 2 then medicaid = 1; else medicaid = 0;
   if pay1 = 3 then private = 1; else private = 0;
   if pay1 = 4 then self_pay = 1; else self_pay = 0;
   if pay1 not in (2,3,4) then other_ins = 1; else other_ins = 0;
   if (months_23 >= -40 and months_23 <= 40) or bad_date = 1;
run;
proc sort data = junk1; by DSHOSPID; run;
proc sort data = &lib..&file2. (keep = DSHOSPID AHAID) out = junk2;  by AHAID; run;
data junk3;
   merge junk2 (in = ina) AHA.&file3.;
   by AHAID;
   if ina = 1; 
run;

proc sort data = junk3;   by DSHOSPID; run;

data &file1. (drop = DSHOSPID);
   merge junk1 (in = ina) junk3;
   by DSHOSPID; 
   if ina = 1;
	if bad_date = 1 then delete;
    ad_date = mdy(amonth,15,ayear);
	if state in ('IA','WI') and ad_date < mdy(1,1,2004) then delete;
	visit = 1;
   if cntrl in (12,13,14,15,16) then cntrl_public = 1; else cntrl_public = 0;
   if cntrl in (21,23) then cntrl_non_profit = 1; else cntrl_non_profit = 0;
   if cntrl in (31,32,33) then cntrl_for_profit = 1; else cntrl_for_profit = 0;
   if cntrl not in (12,13,14,15,16,21,23,31,32,33) then cntrl_other = 1; else cntrl_other = 0;
   if female = 1 then male = 0;
   if female = 0 then male = 1;
run;
proc datasets;
   delete junk1 junk2 junk3;
run;
%mend;

%pull_s(IA_INP,ia_sidc_2004_core,ia_sidc_2004_ahal,Aha_extract2004,asource,byear);
%pull_s(IA_INP,ia_sidc_2005_core,ia_sidc_2005_ahal,Aha_extract2005,asource,byear);
%pull_s(IA_INP,ia_sidc_2006_core,ia_sidc_2006_ahal,Aha_extract2006,asource,byear);
%pull_s(IA_INP,ia_sidc_2007_core,ia_sidc_2007_ahal,Aha_extract2007,asource,PointOfOriginUB04);
%pull_s(IA_INP,ia_sidc_2008_core,ia_sidc_2008_ahal,Aha_extract2008,byear,PointOfOriginUB04);  
%pull_s(IA_INP,ia_sidc_2009_core,ia_sidc_2009_ahal,Aha_extract2009,byear,PointOfOriginUB04);  

%pull_s(WI_INP,wi_sidc_2004_core,wi_sidc_2004_ahal,Aha_extract2004,asource,byear);
%pull_s(WI_INP,wi_sidc_2005_core,wi_sidc_2005_ahal,Aha_extract2005,asource,byear);
%pull_s(WI_INP,wi_sidc_2006_core,wi_sidc_2006_ahal,Aha_extract2006,asource,byear);
%pull_s(WI_INP,wi_sidc_2007_core,wi_sidc_2007_ahal,Aha_extract2007,asource,byear);
%pull_s(WI_INP,wi_sidc_2008_core,wi_sidc_2008_ahal,Aha_extract2008,byear,PointOfOriginUB04);  
%pull_s(WI_INP,wi_sidc_2009_core,wi_sidc_2009_ahal,Aha_extract2009,byear,PointOfOriginUB04);   

data iowa_inp;
   set ia_sidc_2004_core  ia_sidc_2005_core  ia_sidc_2006_core  ia_sidc_2007_core  ia_sidc_2008_core  ia_sidc_2009_core;
run;
data wisconsin_inp; 
   set wi_sidc_2004_core  wi_sidc_2005_core  wi_sidc_2006_core  wi_sidc_2007_core  wi_sidc_2008_core  wi_sidc_2009_core;
run;

proc means data = wisconsin_inp mean;
   var male female preg_related cntrl_public cntrl_non_profit cntrl_for_profit medicaid private self_pay;
Run;
proc means data = iowa_inp mean;
   var male female preg_related cntrl_public cntrl_non_profit cntrl_for_profit medicaid private self_pay;
Run;

%macro stab(i_file,o_file);
proc means data = &i_file. noprint;
   var visit male female preg_related from_ED medicaid private self_pay other_ins cntrl_public cntrl_non_profit cntrl_for_profit cntrl_other;
   output out = junk1 sum = visit male female preg_related from_ED medicaid private self_pay other_ins cntrl_public cntrl_non_profit cntrl_for_profit cntrl_other;
   class ad_date;
   where months_23 >= -12 and months_23 <=12;
run;
data junk2;
   format ad_date date.;
   set junk1;
   if _type_ = 0 then delete;
run; 
PROC EXPORT DATA= junk2
            OUTFILE= "C:\Research\Insurance 23\3. Code up from Scratch\&o_File..csv"
            DBMS=CSV REPLACE;
RUN;
proc datasets;
   delete junk1 junk2;
run;
%mend;

%stab(iowa_inp,P04 Iowa Inpatient);
%stab(wisconsin_inp,P04 Wisconsin Inpatient);

%macro pull_v(suf,a_where);
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk1 (rename=(_FREQ_ = &st._&suf.)) mean =  &st._medi_&suf. &st._priv_&suf. &st._self_&suf. &st._o_ins_&suf.;
   class months_23;
   where state = "&st." &a_where.;
run;
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk2 (rename=(_FREQ_ = &st._&suf._ED)) mean =  &st._medi_&suf._ED &st._priv_&suf._ED &st._self_&suf._ED &st._o_ins_&suf._ED;
   class months_23;
   where state = "&st." and from_ED = 1 &a_where.;
run;
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk3 (rename=(_FREQ_ = &st._&suf._NED)) mean =  &st._medi_&suf._NED &st._priv_&suf._NED &st._self_&suf._NED &st._o_ins_&suf._NED;
   class months_23;
   where state = "&st." and from_ED = 0 &a_where.;
run;
data &suf.;
   merge junk1 junk2 junk3;
   by months_23;
run;
proc datasets;
   delete junk1 junk2 junk3;
run;
%mend;

%macro p_state(st,i_File);
   %pull_v(all,);
   %pull_v(all_pub,and cntrl_public = 1);
   %pull_v(all_non_prof,and cntrl_non_profit = 1);
   %pull_v(all_for_prof,and cntrl_for_profit = 1);
   %pull_v(all_other,and cntrl_other = 1);
   %pull_v(all_np,and preg_related = 0);
   %pull_v(all_np_pub,and preg_related = 0 and cntrl_public = 1);
   %pull_v(all_np_non_prof,and preg_related = 0 and cntrl_non_profit = 1);
   %pull_v(all_np_for_prof,and preg_related = 0 and cntrl_for_profit = 1);
   %pull_v(all_np_other,and preg_related = 0 and cntrl_other = 1);
   %pull_v(female,and male = 0);
   %pull_v(female_pub,and male = 0 and cntrl_public = 1);
   %pull_v(female_non_prof,and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_for_prof,and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_other,and male = 0 and cntrl_other = 1);
   %pull_v(female_np,and preg_related = 0 and male = 0);
   %pull_v(female_np_pub,and preg_related = 0 and male = 0 and cntrl_public = 1);
   %pull_v(female_np_non_prof,and preg_related = 0 and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_np_for_prof,and preg_related = 0 and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_np_other,and preg_related = 0 and male = 0 and cntrl_other = 1);
   %pull_v(female_p,and preg_related = 1 and male = 0);
   %pull_v(female_p_pub,and preg_related = 1 and male = 0 and cntrl_public = 1);
   %pull_v(female_p_non_prof,and preg_related = 1 and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_p_for_prof,and preg_related = 1 and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_p_other,and preg_related = 1 and male = 0 and cntrl_other = 1);
   %pull_v(male,and male = 1);
   %pull_v(male_pub,and male = 1 and cntrl_public = 1);
   %pull_v(male_non_prof,and male = 1 and cntrl_non_profit = 1);
   %pull_v(male_for_prof,and male = 1 and cntrl_for_profit = 1);
   %pull_v(male_other,and male = 1 and cntrl_other = 1);
   data inter.ins_23_reprod_&ST. (drop = _type_ );
      merge all 		all_pub 		all_non_prof 		all_for_prof 		all_other
       	 	all_np 		all_np_pub 		all_np_non_prof 	all_np_for_prof 	all_np_other
         	female 		female_pub 		female_non_prof 	female_for_prof 	female_other
   		 	female_np 	female_np_pub 	female_np_non_prof 	female_np_for_prof 	female_np_other
   		 	female_p 	female_p_pub 	female_p_non_prof 	female_p_for_prof 	female_p_other
   		 	male 		male_pub 		male_non_prof 		male_for_prof 		male_other;
      by months_23;
      if months_23 = . then delete;
      merge_v = 1;
   run;
%mend;

%p_state(IA,iowa_inp);
%p_state(WI,wisconsin_inp);

proc datasets;
   delete all 		all_pub 		all_non_prof 		all_for_prof 		all_other
       	 	all_np 		all_np_pub 		all_np_non_prof 	all_np_for_prof 	all_np_other
         	female 		female_pub 		female_non_prof 	female_for_prof 	female_other
   		 	female_np 	female_np_pub 	female_np_non_prof 	female_np_for_prof 	female_np_other
   		 	female_p 	female_p_pub 	female_p_non_prof 	female_p_for_prof 	female_p_other
   		 	male 		male_pub 		male_non_prof 		male_for_prof 		male_other
     ia_sidc_2004_core  ia_sidc_2005_core  ia_sidc_2006_core  
     ia_sidc_2007_core  ia_sidc_2008_core  ia_sidc_2009_core
     wi_sidc_2004_core  wi_sidc_2005_core  wi_sidc_2006_core  
     wi_sidc_2007_core  wi_sidc_2008_core  wi_sidc_2009_core;
run;
